package ar.com.ford.it.fullEconomics.persistence.dao.query;

import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp02Commodity;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class CommodityByNameFindQuery implements CustomExpressionFactory {

    public Expression createExpression(ExpressionBuilder builder,
                                       Object[] parameters) {
        Expression exp = null;
        Teesp02Commodity commodity = (Teesp02Commodity) parameters[0];
        
        String name = commodity.getEesp02CommodityNameN();
        Teesg01Plant plant = commodity.getEesp02PlantCodeC();

        exp = builder.get("eesp02PlantCodeC").equal(plant);
        if (name != null) {
            exp = exp.and(builder.get("eesp02CommodityNameN").toLowerCase().equal(
                    name.toLowerCase()));
        }
        return exp;
    }

}
